package org.clockwise.driver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.clockwise.feature.Query;
import org.clockwise.feature.QueryContext;

import Listwise.PredictLinearNeuralModel;
import Listwise.TrainLinearNeuralModel;

public class ListwiseTrainDriver {
	public static int test(List<QueryContext> train, List<QueryContext> test) {
		List<List<double[]>> data = new ArrayList<List<double[]>>();
		Iterator<QueryContext> iter = train.iterator();
		while (iter.hasNext()) {
			List<Query> ql = iter.next().queryList;
			for (int i = 0; i < ql.size(); i++) {
				if (ql.get(i).isLinked && ql.get(i).findCorrect)
					data.add(ql.get(i).featureList);
			}
		}
		double[] w = TrainLinearNeuralModel.train(data);
		
		iter = test.iterator();
		int acc = 0;
		int all = 0;
		while (iter.hasNext()) {
			List<Query> ql = iter.next().queryList;
			all += ql.size();
			for (int i = 0; i < ql.size(); i++) {
				if (ql.get(i).isLinked && ql.get(i).findCorrect) {
					int p = PredictLinearNeuralModel.predict(w, ql.get(i).featureList);
					if (p == 0) {
						acc++;
					}
				}
			}
		}
		return acc;
	}
}
